local neifa = fk.CreateSkill{
    name = "wk_heg__neifa",
}

Fk:loadTranslationTable{
  ["wk_heg__neifa"] = "内伐",
  [":wk_heg__neifa"] = "你可将两张颜色不同的牌当【勠力同心】使用，此牌结算后若你因此横置，你可以对一名横置的角色造成1点伤害，若没有角色因此横置，此技能失效至本回合结束。",

  ["#wk_heg__neifa"] = "内伐：你可将两张颜色不同的牌当【勠力同心】使用",
  ["#wk_heg__neifa-choose"] = "内伐：你可以选择对一名横置的角色造成1点伤害",

  ["$wk_heg__neifa1"] = "自相恩残，相煎何急。",
  ["$wk_heg__neifa2"] = "同室内伐，贻笑外人。",
}

neifa:addEffect("viewas",{
    anim_type = "special",
    pattern = "fight_together",
    prompt = "#wk_heg__neifa",
    card_filter = function(self, player, to_select, selected)
      if #selected == 0 then
        return true
      elseif #selected == 1 then
        return Fk:getCardById(to_select):compareColorWith(Fk:getCardById(selected[1]), true)
      else
        return false
      end
    end,
    view_as = function(self, player, cards)
      if #cards ~= 2 then
        return nil
      end
      local c = Fk:cloneCard("fight_together")
      c.skillName = neifa.name
      c:addSubcards(cards)
      return c
    end,
    enabled_at_play = function(self, player)
      return player:getMark("wk_heg__neifa-turn") == 0
    end,
})

neifa:addEffect(fk.CardUseFinished,{
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:getMark("wk_heg__neifa_damage") == 1 and player.chained and data.card and table.contains(data.card.skillNames, "wk_heg__neifa")
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local targets = table.filter(room.alive_players, function(p) return p.chained end)
      if #targets > 0 then
        local tos = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#wk_heg__neifa-choose",
            skill_name = neifa.name
        })
        if #tos > 0 then
          local to = tos[1]
          room:damage{
            from = player,
            to = to,
            damage = 1,
            skillName = neifa.name,
          }
        end
      end
      room:setPlayerMark(player, "wk_heg__neifa_damage", 0)
    end,
})

neifa:addEffect(fk.TargetConfirmed,{
    can_refresh = function(self, event, target, player, data)
      return data.card and table.contains(data.card.skillNames, "wk_heg__neifa")
        and (not table.find(data.use.tos, function(p) return not p.chained end) or not player.chained)
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      if not table.find(data.use.tos, function(p) return not p.chained end) then
        room:setPlayerMark(player, "wk_heg__neifa-turn", 1)
      end
      if not player.chained then
        room:setPlayerMark(player, "wk_heg__neifa_damage", 1)
      end
    end,
})

return neifa